package orders

import (
	"context"
	"fmt"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/dal"
	"mall_admin_api/biz/model/admin_orders"
	"mall_admin_api/biz/model/common"
	"mall_admin_api/biz/pack"
	"mall_admin_api/biz/util"
	"time"
)

func (c *Orders) GetOrdersList(ctx context.Context, req *admin_orders.GetOrderListRequest, page, limit int) ([]*admin_orders.Order, int64, error) {
	list, total, err := c.dao.OrderDAO.GetListWithTotal(ctx, &dal.OrderListParams{
		Condition: nil,
		Page:      page,
		Size:      limit,
		Sort:      []field.Expr{dal.Order.CreatedAt.Desc()},
	})
	if err != nil {
		hlog.CtxErrorf(ctx, "GetOrdersList err: %v", err)
		return nil, 0, err
	}

	var userIDs []int64
	for _, order := range list {
		userIDs = append(userIDs, order.UserID)
	}
	userIDs = util.Unique(userIDs)
	userMap, err := c.dao.UserDAO.MGetUser(ctx, userIDs)
	if err != nil {
		hlog.CtxErrorf(ctx, "MGetUserInfo err: %v", err)
		return nil, 0, err
	}
	return pack.GetOrderList(list, userMap), total, nil
}

func (c *Orders) UpdateOrders(ctx context.Context, req *admin_orders.UpdateOrderRequest) error {
	var fields []field.AssignExpr
	fields = append(fields, dal.Order.UpdatedBy.Value(c.commonInfo.GetAdministratorInfo().Nickname))
	if err := c.dao.OrderDAO.UpdateOrder(ctx, []gen.Condition{dal.Order.ID.Eq(req.ID)}, fields); err != nil {
		hlog.CtxErrorf(ctx, "UpdateOrders err: %v", err)
		return err
	}
	return nil
}

func (c *Orders) Info(ctx context.Context, req *admin_orders.GetOrderInfoRequest) (*admin_orders.Order, error) {
	info, err := c.dao.OrderDAO.GetOrderInfoByID(ctx, req.GetID())
	if err != nil {
		hlog.CtxErrorf(ctx, "Info err: %v", err)
		return nil, err
	}
	return pack.Order(info, nil), nil
}

func (c *Orders) Detail(ctx context.Context, req *admin_orders.GetOrderDetailRequest) (*admin_orders.OrderDetail, error) {
	info, err := c.dao.OrderDAO.GetOrderInfoByID(ctx, req.GetID())
	if err != nil {
		hlog.CtxErrorf(ctx, "dal GetOrderInfo err: %v", err)
		return nil, err
	}
	orderItemList, err := c.dao.OrdersItemDAO.GetOrdersItemList(ctx, []gen.Condition{dal.OrdersItem.OrderID.Eq(req.GetID())}, 0, 1000)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal GetOrderItemListByCondition err: %v", err)
		return nil, err
	}
	return pack.OrderDetail(info, orderItemList), nil
}

func (c *Orders) DeliveryOrder(ctx context.Context, req *admin_orders.DeliveryOrderRequest) error {
	var fields []field.AssignExpr
	fields = append(fields,
		dal.Order.Status.Value(common.OrderStatusPtr(common.OrderStatus_DeliveryCompleted)),
		dal.Order.DeliveryCompanyID.Value(req.GetDeliveryCompanyID()),
		dal.Order.DeliveryNumber.Value(req.GetDeliveryNumber()),
		dal.Order.DeliveryTime.Value(time.Now()),
	)
	if err := c.dao.OrderDAO.UpdateOrder(ctx, []gen.Condition{dal.Order.ID.Eq(req.GetOrderID())}, fields); err != nil {
		hlog.CtxErrorf(ctx, "DeliverOrder err: %v", err)
		return err
	}

	c.SyncLogOrdersLog(ctx, req.OrderID, "发货成功:"+util.GetJsonStyle(req))

	return nil
}

func (c *Orders) RemarkOrder(ctx context.Context, req *admin_orders.RemarkOrderRequest) error {
	var fields []field.AssignExpr
	fields = append(fields,
		dal.Order.Remark.Value(req.GetRemark()),
	)
	if err := c.dao.OrderDAO.UpdateOrder(ctx, []gen.Condition{dal.Order.ID.Eq(req.GetID())}, fields); err != nil {
		hlog.CtxErrorf(ctx, "ReamrkOrder err: %v", err)
		return err
	}

	c.SyncLogOrdersLog(ctx, req.ID, fmt.Sprintf("添加备注：%s", req.Remark))

	return nil
}
